package com.nuumoblie.bluetoothtools

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.nuumoblie.bluetoothtools.utils.OnPermissionCallback
import es.dmoral.toasty.Toasty
import com.tbruyelle.rxpermissions2.RxPermissions
import android.view.WindowManager
import com.nuumobile.filepicker.controller.DialogSelectionListener
import com.nuumobile.filepicker.model.DialogProperties
import com.nuumobile.filepicker.view.FilePickerDialog


abstract class BaseActivity : AppCompatActivity() {

    companion object {
        val LOCATION_REQUEST_PERMISSION_CODE: Int = 0;
        val STORAGE_OPT_REQUEST_PERMISSION_CODE: Int = 1;
        val PERMISSION_LENGTH: Int = 2;
    }

    private val permissionCallbacks = arrayOfNulls<OnPermissionCallback>(PERMISSION_LENGTH)
    val rxPermissions: RxPermissions by lazy { RxPermissions(this); };
    override fun onCreate(savedInstanceState: Bundle?) {
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        super.onCreate(savedInstanceState)
        setContentView(getContentLayoutId());
        initToolbar();
        initView();
        initData()
        initEvent();
    }

    @LayoutRes
    abstract fun getContentLayoutId(): Int;

    open fun initToolbar(){}

    open fun initView(): Unit {

    }

    open fun initEvent(): Unit {

    }

    open fun initData() {};

    fun checkLocationPermission(callback: OnPermissionCallback) {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                LOCATION_REQUEST_PERMISSION_CODE
            );
        } else {
            callback.onPermissionGranted();
        }
    }

    fun checkStoragePermission(callback: OnPermissionCallback) {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ),
                STORAGE_OPT_REQUEST_PERMISSION_CODE
            );
        } else {
            callback.onPermissionGranted();
        }
    }

    fun checkAndEnableBt() {
        val adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter == null) {
            Toasty.error(this, "this device unsupport bluetooth", Toast.LENGTH_SHORT, true).show();
            finish();
            return;
        }

        if (!adapter.isEnabled) {
//            adapter.enable();
            enableBluetooth(this, 0);
        }
    }

    fun enableBluetooth(activity: AppCompatActivity, requestCode: Int) {
        val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        activity.startActivityForResult(intent, requestCode)
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == LOCATION_REQUEST_PERMISSION_CODE) {

        } else if (requestCode == STORAGE_OPT_REQUEST_PERMISSION_CODE) {

        }

    }

    open fun selectFile(callbacks:DialogSelectionListener) {
        val properties = DialogProperties()
        properties.extensions = arrayOf("")
        val dialog = FilePickerDialog(this, properties)
        dialog.setTitle("Select a file")
        dialog.setPositiveBtnName("Select")
        dialog.setNegativeBtnName("Cancel")
        dialog.setDialogSelectionListener(callbacks)
        dialog.show()
    }

}